using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using TX.Data.DSTableAdapters;

namespace TX
{
    public partial class Customer : Base2
    {
        public Customer()
        {
            InitializeComponent();

            tableAdapterManager.CustomerTableAdapter = new CustomerTableAdapter();
            tableAdapterManager.CustomerAddressTableAdapter = new CustomerAddressTableAdapter();
            tableAdapterManager.AddressTableAdapter = new AddressTableAdapter();
            tableAdapterManager.SalesAgentCustomerTableAdapter = new SalesAgentCustomerTableAdapter();
        }


        private void Customer_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dSSalesAgent.SalesAgent' table. You can move, or remove it, as needed.
            this.salesAgentTableAdapter1.Fill(this.dSSalesAgent.SalesAgent);
            try
            {
                gridView1.BeginUpdate();
                bs1 = new BindingSource(ds, "Customer");

                bindingNavigator1.BindingSource = bs1;

                ds.Customer.BeginLoadData();
                tableAdapterManager.CustomerTableAdapter.Fill(ds.Customer);
                ds.Customer.EndLoadData();

                CountryCodeLookUpEdit.Properties.Assign(riCountry);

                //dataLayoutControl1.OptionsView.HighlightFocusedItem = true;
                gridControl1.DataSource = this.bs1;
                InitGridView(gridView1, true);
                gridView1.BestFitColumns();

                //this.gridView1.OptionsBehavior.Editable = false;
                //this.gridView1.OptionsView.ColumnAutoWidth = false;
                //this.gridView1.OptionsView.EnableAppearanceOddRow = true;
                //this.gridView1.OptionsView.ShowAutoFilterRow = true;
                //gridView1.UnselectRow(1);
                //this.gridView1.OptionsSelection.EnableAppearanceFocusedRow = false;
                gridView1.EndUpdate();


                bs2 = new BindingSource(bs1, "FK_CustomerAddress_Customer_CustomerId");
                bs3 = new BindingSource(ds, "Address");
                bs4 = new BindingSource(bs1, "FK_SalesAgentCustomer_Customer");
                //bs5 = new BindingSource(ds, "SalesAgent");

                gridControl2.DataSource = bs2;
                gridControl3.DataSource = bs4;

                InitGridView(gridView2);
                gridView2.Columns["CustomerId"].Visible = false;
                gridView2.Columns["AddressId"].Visible = false;

                InitGridView(gridView3);
                //gridView3.Columns["CustomerId"].Visible = false;
                //gridView3.Columns["SalesAgentId"].Visible = false;

                DLCBind("dataLayoutControl1", bs3);
                //DLCBind("dataLayoutControl2", bs5);
                //SalesAgentIdGridLookUpEdit.Properties.Assign(riSalesAgent);

                CurrentChanged();

                bs1.CurrentChanged += new EventHandler(bs1_CurrentChanged);
                bs2.AddingNew += new AddingNewEventHandler(bs2_AddingNew);
                bs2.CurrentChanged += new EventHandler(bs2_CurrentChanged);
                ds.CustomerAddress.RowDeleting += new DataRowChangeEventHandler(CustomerAddress_RowDeleting);

                //bs4.AddingNew += new AddingNewEventHandler(bs4_AddingNew);
                //bs4.CurrentChanged += new EventHandler(bs4_CurrentChanged);
                //ds.SalesAgentCustomer.RowDeleting += new DataRowChangeEventHandler(SalesAgentCustomer_RowDeleting);
            }
            catch (Exception Ex)
            {
                //throw;
            }
        }


        int customerId;

        void bs1_CurrentChanged(object sender, EventArgs e)
        {
            Save();
            CurrentChanged();
        }

        void bs2_CurrentChanged(object sender, EventArgs e)
        {
            if (bs2.Current != null)
            {
                if (((DataRowView)bs2.Current).Row["AddressId"] == DBNull.Value)
                {
                    ((DataRowView)bs2.Current).Row["AddressId"] = ((DataRowView)bs3.Current).Row["AddressId"];
                    ((DataRowView)bs2.Current).Row["ModifiedDate"] = DateTime.Now;
                }
                else
                    bs3.Filter = "AddressId = " + ((DataRowView)bs2.Current).Row["AddressId"].ToString();

            }
        }

        void bs2_AddingNew(object sender, AddingNewEventArgs e)
        {
            bs3.AddNew();
            ((DataRowView)bs3.Current).Row["ModifiedDate"] = DateTime.Now;
        }

        void CustomerAddress_RowDeleting(object sender, DataRowChangeEventArgs e)
        {
            DataRow[] ToDeleted = ds.Address.Select("AddressId=" + e.Row["AddressId"].ToString(), "");
            foreach (DataRow dr in ToDeleted)
                dr.Delete();
        }


        //void bs4_CurrentChanged(object sender, EventArgs e)
        //{
        //    if (bs4.Current != null)
        //    {
        //        if (((DataRowView)bs4.Current).Row["SalesAgentId"] == DBNull.Value)
        //        {
        //            ((DataRowView)bs4.Current).Row["SalesAgentId"] = ((DataRowView)bs5.Current).Row["SalesAgentId"];
        //        }
        //        else
        //            bs5.Filter = "SalesAgentId = " + ((DataRowView)bs4.Current).Row["SalesAgentId"].ToString();

        //    }
        //}

        //void bs4_AddingNew(object sender, AddingNewEventArgs e)
        //{
        //    bs5.AddNew();
        //    ((DataRowView)bs5.Current).Row["ModifiedDate"] = DateTime.Now;
        //}

        //void SalesAgentCustomer_RowDeleting(object sender, DataRowChangeEventArgs e)
        //{
        //    DataRow[] ToDeleted = ds.SalesAgent.Select("SalesAgentId=" + e.Row["SalesAgentId"].ToString(), "");
        //    foreach (DataRow dr in ToDeleted)
        //        dr.Delete();
        //}


        void CurrentChanged()
        {
            if (bs1.Current != null)
            {
                customerId = int.Parse(((DataRowView)bs1.Current).Row["CustomerId"].ToString());
                this.Text = "Customer : " + ((DataRowView)bs1.Current).Row["Name"].ToString();

                //bs3.Filter = null;
                //if (((DataRowView)bs1.Current).Row["CustomerId"] != DBNull.Value)
                //    bs3.Filter = "CustomerId = " + customerId.ToString();
            }

            RefreshData();
        }


        private void RefreshData()
        {
            ds.EnforceConstraints = false;
            //customerAddressTableAdapter.ClearBeforeFill = true;
            //customerAddressTableAdapter.FillByCustomerId(ds.CustomerAddress, customerId);
            //gridView2.BestFitColumns();

            //addressTableAdapter.ClearBeforeFill = true;
            //addressTableAdapter.FillByCustomerId(ds.Address, customerId);

            //salesAgentCustomerTableAdapter.ClearBeforeFill = true;
            //salesAgentCustomerTableAdapter.FillByCustomerId(ds.SalesAgentCustomer, customerId);
            gridView3.BestFitColumns();
        }


        public override void Save2()
        {
            //DataRow[] rowsToSubmit;
            //rowsToSubmit = ds.Customer.Select("", "", Added);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.customerTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.Address.Select("", "", Added);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.addressTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.CustomerAddress.Select("", "", Added);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.customerAddressTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.SalesAgentCustomer.Select("", "", Added);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.salesAgentCustomerTableAdapter.Update(rowsToSubmit);

            ////Submit deleted orders
            //rowsToSubmit = ds.SalesAgentCustomer.Select("", "", Deleted);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.salesAgentCustomerTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.CustomerAddress.Select("", "", Deleted);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.customerAddressTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.Address.Select("", "", Deleted);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.addressTableAdapter.Update(rowsToSubmit);

            //rowsToSubmit = ds.Customer.Select("", "", Deleted);
            //if (rowsToSubmit.Length != 0) numberOfChangesSubmitted += this.customerTableAdapter.Update(rowsToSubmit);

            //int pos2 = bs2.Position;
            //int pos4 = bs4.Position;
            //ds.AcceptChanges();
            //bs2.Position = pos2;
            //bs4.Position = pos4;
        }




        private void gridView2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Delete row?", "Confirmation", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    return;
                GridView view = sender as GridView;
                view.DeleteRow(view.FocusedRowHandle);
            }
        }

        private void SalesAgentIdSpinEdit_DoubleClick(object sender, EventArgs e)
        {
                //            EFatura_Yeni efatura = new EFatura_Yeni(((TX.Data.DSx1.FaturaRow)((System.Data.DataRowView)gridView1.GetFocusedRow()).Row).FaturaId);
            //SalesAgent salesAgent = new SalesAgent(int.Parse(((System.Data.DataRowView)gridView3.GetFocusedRow()).Row["SalesAgentId"].ToString()));
            //SalesAgent salesAgent = new SalesAgent(new BindingSource(bs5, "FK_SalesAgentCustomer_SalesAgent"));
            SalesAgent salesAgent = new SalesAgent();
            MdiShow(salesAgent, this.ParentForm);
        }

    }
}